package com.gxa.jjty.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.mapper.ReturnServiceMapper;
import com.gxa.jjty.admin.mapper.ReturnVOMapper;
import com.gxa.jjty.admin.service.ReturnVOService;
import com.gxa.jjty.core.pojo.ReturnService;
import com.gxa.jjty.core.pojo.ReturnVO;
import com.gxa.jjty.core.service.impl.BaseServiceImpl;
import com.gxa.jjty.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ReturnVOServiceImpl
 * @Author: ws
 * @date 2021/12/9 16:57
 */
@Service("ReturnVOService")
public class ReturnVOServiceImpl extends BaseServiceImpl<ReturnVO> implements ReturnVOService {

    @Autowired
    ReturnVOMapper returnVOMapper;

    @Autowired
    ReturnServiceMapper returnServiceMapper;

    @Override
    public PageInfo<ReturnVO> selectByPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        Example example = new Example(ReturnVO.class);

        //实现排序
        example.orderBy("state");
        example.orderBy("orderId").desc();
        List<ReturnVO> ts = returnVOMapper.selectByExample(example);


        PageInfo<ReturnVO> list = PageInfo.of(ts);
        return list;
    }

    @Override
    public PageInfo<ReturnVO> selectByTimeAnd(Integer page, Integer size, Timestamp from, Timestamp to,
                                              String name, Boolean state) throws IllegalAccessException {
        PageHelper.startPage(page, size);
        List<ReturnVO> ts = null;
        Example example = new Example(ReturnVO.class);

        Example.Criteria criteria = example.createCriteria();

        if (!StringUtil.isEmpty(name)) {
            criteria.andLike("orderUser", "%" + name + "%");
        }
        if (state != null) {
            criteria.andEqualTo("state", state);
        }
        if (from != null && to != null) {
            criteria.andBetween("orderTime", from, to);
        }
        //实现排序
        example.orderBy("state");
        example.orderBy("orderId").desc();
        ts = returnVOMapper.selectByExample(example);


        PageInfo<ReturnVO> list = PageInfo.of(ts);
        return list;
    }

    @Override
    public Map selectCount() {

        // 所有已完成的订单总数
        List<ReturnVO> all = returnVOMapper.selectAll();
        long orderSize = all.size();

        // 已回访的总数
        Example example = new Example(ReturnVO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("state", true);
        List<ReturnVO> trueAll = returnVOMapper.selectByExample(example);
        long trueSize = trueAll.size();

        // 创建一个数据格式化对象
        NumberFormat instance = null;
        // 回访率(回访数除以订单数)
        String proportion = "0";
        if (orderSize > 0) {
            // 实例数据格式化对象
            instance = NumberFormat.getInstance();
            // 设置精确小数位数
            instance.setMaximumFractionDigits(0);
            proportion = instance.format((double) trueSize / (double) orderSize*100) + "%";
        }

        // 满意度()
        String satisfaction = "0";
        Long satiSum = returnServiceMapper.selectBySati();
        if (trueSize > 0 ){
            instance = NumberFormat.getInstance();
            instance.setMaximumFractionDigits(2);
            satisfaction = instance.format((double) satiSum / (double) trueSize);
        }

        Map<String, Object> map = new HashMap<>(3);
        map.put("orderSize", orderSize);
        map.put("trueSize", trueSize);
        map.put("proportion", proportion);
        map.put("satisfaction", satisfaction);

        return map;
    }

    @Override
    public Integer updateReturn(ReturnService returnService) {

        returnService.setCreatedTime(new Timestamp(System.currentTimeMillis()));
        returnService.setState(true);

        Example example = new Example(ReturnService.class);
        example.createCriteria().andEqualTo("orderId",returnService.getOrderId());

        Integer i = returnServiceMapper.updateByExampleSelective(returnService, example);

        return i;
    }

    @Override
    public List<ReturnService> getReturnService(ReturnService returnService) {

        Example example = new Example(ReturnService.class);
        example.createCriteria().andEqualTo("orderId", returnService.getOrderId());
        List<ReturnService> returnServices = returnServiceMapper.selectByExample(example);
        return returnServices;
    }
}
